﻿using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcComboBox
    {
        public static readonly DependencyProperty DescribeProperty = DependencyProperty.Register("Describe", typeof(string), typeof(UcComboBox), new PropertyMetadata("Describe", DescribeChangedCallback));

        private static void DescribeChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UcComboBox cbb)
            {
                cbb.DescribeChanged(e.NewValue + "");
            }
        }

        public int LabelMinWidth
        {
            get { return (int)GetValue(LabelMinWidthProperty); }
            set { SetValue(LabelMinWidthProperty, value); }
        }

        public static readonly DependencyProperty LabelMinWidthProperty =
            DependencyProperty.Register("LabelMinWidth", typeof(int), typeof(UcComboBox), new PropertyMetadata(0));

        public string Describe
        {
            get => (string)GetValue(DescribeProperty);
            set => SetValue(DescribeProperty, value);
        }

        public int SelectedIndex
        {
            get =>Cbb.SelectedIndex;
            set => Cbb.SelectedIndex = value;
        }

        public object SelectedValue
        {
            get => Cbb.SelectedValue;
            set => Cbb.SelectedValue =value;
        }

        public object SelectedItem
        {
            get => Cbb.SelectedItem;
            set => Cbb.SelectedItem = value;
        }

        public event SelectionChangedEventHandler SelectionChanged;

        public ComboBox CbbChildBox => Cbb;

        private readonly Timer m_toolTipClosingTimer;

        public UcComboBox()
        {
            InitializeComponent();
            DataContext = this;
            m_toolTipClosingTimer = new Timer(ToolTipClosingCallBack, null, Timeout.Infinite, Timeout.Infinite);
        }

        public void Init<T>(IEnumerable<T> source, string text, string value)
        {
            Cbb.ItemsSource = source;
            Cbb.DisplayMemberPath = text;
            Cbb.SelectedValuePath = value;
            //Cbb.SelectedIndex = 0;
            //Cbb.SelectionChanged += Cbb_SelectionChanged;
        }

        private void Cbb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Tooltip.IsOpen = false;
            SelectionChanged?.Invoke(sender,e);
        }

        public void DescribeChanged(string label)
        {
            TxtttDesc.Text = label;
            TxtttName.Text = Tag + "";
        }

        private void UcComboBox_OnLostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TxtttDesc.Text)) return;
             Tooltip.IsOpen = false;
        }

        private void ToolTipClosingCallBack(object o)
        {
            Dispatcher.Invoke(() =>
            {
                Tooltip.IsOpen = false;
            });
        }

        private void UcComboBox_OnTouchEnter(object sender, TouchEventArgs e)
        {
            Tooltip.IsOpen = true;
        }

        private void UcComboBox_OnTouchLeave(object sender, TouchEventArgs e)
        {
            m_toolTipClosingTimer.Change(5000, Timeout.Infinite);
        }
    }
}

